--=======================================
--[[ 本地函数 ]]
--=======================================
-- 移除预制物(预制物,数量)
local function removeItem(item, num)
    if item.components.stackable then
        item.components.stackable:Get(num):Remove()
    else
        item:Remove()
    end
end
--=======================================
--[[ 动作定义 ]]
--=======================================
local actions = {
    NUTS_DUSTING = {
        id = "NUTS_DUSTING",
        priority = 6,
        strfn = function(act)
            return "GENERIC"
        end,
        fn = function(act)
            if act.target:HasTag("spicedfood") then
                return false, "NUTS_DUSTING_FAIL"
            else
                local owner = act.target.components.inventoryitem and act.target.components.inventoryitem.owner or nil
                local container = nil --目标料理所在容器组件
                if owner then
                    container = owner.components.inventory or owner.components.container
                end

                local newfood = SpawnPrefab(act.target.prefab .. "_" .. act.invobject.prefab)
                local perishablePercent = nil
                if act.target.components.perishable then
                    perishablePercent = act.target.components.perishable:GetPercent()
                end
                if newfood then
                    if newfood.components.perishable and perishablePercent ~= nil then
                        newfood.components.perishable:SetPercent(perishablePercent)
                    end
                    if not (container and container:GiveItem(newfood)) then
                        act.doer.components.inventory:GiveItem(newfood)
                    end
                    removeItem(act.invobject)
                    removeItem(act.target)
                end
                return true
            end
        end,
    },
}

for _, action in pairs(actions) do
    local _action = Action()
    for k, v in pairs(action) do
        _action[k] = v
    end
    AddAction(_action)
end

--=======================================
--[[ 动作行为:就是执行动作的人物动画 ]]
--=======================================
AddStategraphActionHandler("wilson", ActionHandler(ACTIONS.NUTS_DUSTING, "domediumaction"))
AddStategraphActionHandler("wilson_client", ActionHandler(ACTIONS.NUTS_DUSTING, "domediumaction"))

--=======================================
--[[ ADD COMPONENT ACTION ]]
-- SCENE		using an object in the world                                        --args: inst, doer, actions, right
-- USEITEM		using an inventory item on an object in the world                   --args: inst, doer, target, actions, right
-- POINT		using an inventory item on a point in the world                     --args: inst, doer, pos, actions, right, target
-- EQUIPPED		using an equiped item on yourself or a target object in the world   --args: inst, doer, target, actions, right
-- INVENTORY	using an inventory item                                             --args: inst, doer, actions, right
--=======================================

-- target是对象,inst是手中或鼠标上物品,doer是做动作的玩家,right是是否为右键
AddComponentAction("USEITEM", "coffeepowder", function(inst, doer, target, actions, right)
    if inst.components.coffeepowder and target:HasTag("preparedfood") then
        table.insert(actions, ACTIONS.NUTS_DUSTING)
    end
end)

-- 动作兼容行为排队论
local queueractlist = {
    NUTS_DUSTING = "allclick",
}
local actionqueuer_status, actionqueuer_data = pcall(require, "components/actionqueuer")
if actionqueuer_status then
    if AddActionQueuerAction and next(queueractlist) then
        for k, v in pairs(queueractlist) do
            AddActionQueuerAction(v, k, true)
        end
    end
end
